// pages/cultureCoupons/useableStore/index.js
const common = require("../../../public.js")
Page({

    /**
     * 页面的初始数据
     */
    data: {
        dsort: 1,
        storeType: '',
        couponId: '',
        pageNum: 0,
        categoryList: [],
        storeList: [],
        distanceList: [{
            text: '离我最近',
            value: 1
        },
        {
            text: '离我最远',
            value: 2
        }],
        refreshing: false,
        noMoreData: false,
        isLoadingBusy: false,
        type:""
    },
    checkAddress(e) { //点击地址按钮
        // console.log(this.data.storeList,"this.data.storeList.xpoint")
        console.log(e.currentTarget.dataset.xpoint,"66668888999977777")
        let xpoint = e.currentTarget.dataset.xpoint
        let ypoint = e.currentTarget.dataset.ypoint
        let address = e.currentTarget.dataset.address
        let names = e.currentTarget.dataset.names
        if (!xpoint || !ypoint) {
            common.showToast('商家未设置位置信息')
            return
        }
        let lon = parseFloat(xpoint)
        let lat = parseFloat(ypoint)
        wx.openLocation({
            latitude: lat,
            longitude: lon,
            name: names ? names : null,
            address: address ? address : null,
            scale: 18,
            fail: () => {
                wx.showToast({
                    title: '无地址信息',
                    icon: 'none'
                })
            }
        })
    },
    onLoad(opt) {
        const that = this
        console.log(opt,"5458测试数据")
        getApp().getLocation(function (data) {
            that.setData({
                type:opt.type,
                couponId: opt.couponId,
                xpoint: data.longitude,
                ypoint: data.latitude
            }, function () {
                that.getStoreList(true)
            })
        })
        this.getStoreTypeList()
    },
    getStoreList(refresh = false) {
        if (refresh) {
            this.setData({
                refreshing: true,
                noMoreData: false
            })
        } else {
            if (this.data.noMoreData || this.data.isLoadingBusy) {
                return
            }
            this.setData({
                isLoadingBusy: true
            })
        }

        const pageNum = refresh ? 1 : this.data.pageNum + 1

        const that = this
        const tempJson = {
            // couponId: that.data.couponId,
            // regionId: '',
            pageNum,
            pageSize:10,
            dsort: that.data.dsort,
            xpoint: that.data.xpoint,
            ypoint: that.data.ypoint,
            // storeType:''
            storeType: that.data.storeType,
        }
        common.post('/user/getGoldBeansStoreList', tempJson, function (res) {
            if (res.status === 200) {
                res.data.map((item,index)=>{
                     item.distance = (item.distance/1000).toFixed(1)
                })
                that.data.pageNum = pageNum
                if (res.data && res.data.length > 0) {
                    that.setData({
                        storeList: refresh ? res.data : [...that.data.storeList, ...res.data]
                    })
                } else {

                    that.setData({
                        noMoreData: true
                    })
                }
            } else {
                common.showRequestErrMsg(res)
            }
        }, function () {
            that.setData({
                refreshing: false,
                isLoadingBusy: false
            })
        })
    },
    distanceChange(e) {
        const value = e.detail
        this.setData({
            dsort: value
        }, _ => {
            this.getStoreList(true)
        })
    },
    categoryChange(e) {
        const value = e.detail
        this.setData({
            storeType: value,
            storeList:[]
        }, _ => {
            this.getStoreList(true)
        })
    },
        // //获取商品分类
        // getGoodsType() {
        //     const tempJson = {
        //         type: 15
        //     }
        //     common.post('/store/getStoreTypeListForAppByPara', tempJson, res => {
        //         const list = this.chunkArr(res.data, 10)
        //         this.setData({
        //             banner: list
        //         })
        //     })
        // },
    getStoreTypeList() {
        const that = this
        common.post('/user/getGoldBeansStoreType', {}, function (res) {
            if (res.status === 200) {
                let categoryList = res.data.map(function (value, index, array) {
                    return {
                        value: value.id,
                        text: value.name
                    }
                })

                categoryList.unshift({
                    value: '',
                    text: '全部行业'
                })

                that.setData({
                    categoryList
                })
            } else {
                common.showRequestErrMsg(res)
            }
        })
    },
    toStore(e) {
        const item = e.currentTarget.dataset.item
        const storeTypeName = e.currentTarget.dataset.item.storeTypeName
        const id = item.id

        if (storeTypeName === '景区景点') {
            wx.navigateTo({
                url: '/pages/ScenicArea/ScenicAreaMainPage/index?id=' + id + '&moduleType=1',
            })
        } else if (storeTypeName === '文化馆' || storeTypeName === '美术馆' || storeTypeName === '博物馆') {
            wx.navigateTo({
                url: '/pages/ScenicArea/ScenicAreaMainPage/index?id=' + id + '&moduleType=2',
            })
        } else {
            common.gotoStore(item)
        }
    },
    callPhone(e) {
        let mobile = e.currentTarget.dataset.mobile
        wx.makePhoneCall({
            phoneNumber: mobile,
        })
    },
    onReachBottom() {
      
        this.getStoreList(false)
    }
})